class RegisterTagLib {

    static namespace = "pr"

    def ifDevEnv = { attrs, body ->
        def map = grailsApplication.metadata
        String env = map[grails.util.Environment.KEY]
        if (env == grails.util.Environment.DEVELOPMENT) {
            out << body()
        }
    }

    // if pagination...
    def ifPaginate = { attrs, body ->
        def count = attrs.count
        if (params.max && params.max < count) {
            out << body()
        }
    }
    
    // if user logged in...    
    def ifUser = { attrs, body ->
        boolean valid = session.userId != null
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }
    
    // if user same as logged in user...
    def ifSameUser = { attrs, body ->
        boolean valid = session.userId && session.userId == attrs.id
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }
    
    // if logged in user owns this item...
    def ifCurrentUserOwns = { attrs, body ->
        boolean valid = session.userId && session.userId == attrs.id
        if (attrs.not) {
            valid = !valid
        }
        if (valid) {
            out << body()
        }
    }

    
    def userLink = { attrs ->
		def user = User.get(session.userId)
        attrs.controller = 'user'
        attrs.action = 'show'
        attrs.id = user.id
        out << "<a href=\"${createLink(attrs).encodeAsHTML()}\""
        // process remaining attributes
        attrs.each { key, value ->
            out << " $key=\"$value\""
        }
        out << '>'
        out << user.name.encodeAsHTML()
        out << '</a>'
    }
    
    def petAnchor = { attrs, body ->
        def pet = attrs.remove('pet')
        def size = attrs.remove('size')
        out << "<a href=\"${createPetLink(pet.id, size).encodeAsHTML()}\""
        attrs.each { key, value ->
            out << " $key=\"$value\""
        }
        out << '>'
        out << body()
        out << '</a>'
    }
  
    def petImg = { attrs ->
        def pet = attrs.remove('pet')
        def size = attrs.remove('size')
        out << createPetImage(pet, size, attrs)
    }
  
    def petLightboxAnchor = { attrs ->
        def pet = attrs.remove('pet')
        def size = attrs.remove('size')
        def lightboxSize = attrs.remove('lightboxSize')
        def name = pet.name
        if (!name) {
            name = '...'
        }
        name = name.encodeAsHTML()
        StringBuilder sb = new StringBuilder(40)
        sb.append("<a href=\"${createPetLink(name.id, Image.Original).encodeAsHTML()}\"")
        attrs.each { key, value ->
            sb.append(" $key=\"$value\"")
        }
        sb.append('>')
        sb.append(caption.replaceAll(/'/, '&#39;'))
        sb.append('</a>')
        def link = sb.toString().encodeAsHTML()
        out << "<a href=\"${createPetLink(pet.id, lightboxSize).encodeAsHTML()}\" rel=\"lightbox[list]\" title=\"${link}\">"
        out << createPetImage(pet, size, null)
        out << '</a>'
    }
  
    private def createPetImage(pet, size, attrs) {
        Integer width = 0
        Integer height = 0
        switch (size) {
            case Image.Original:
                width = pet.width
                height = pet.height
                break
            default:
                width = Image.Widths[size]
                height = Image.Widths[size]
                break
        } 
        def name = pet.name
        if (!name) {
            name = '...'
        }
        def alt = attrs?.remove('alt')
        if (!alt) {
            alt = name
        }
        alt = alt.encodeAsHTML()
        def title = attrs?.remove('title')
        if (!title) {
            title = name
        }
        title = title.encodeAsHTML()
        StringBuilder sb = new StringBuilder(40)
        sb.append("<img src=\"${createPetLink(pet.id, size).encodeAsHTML()}\" alt=\"$alt\" title=\"$title\" width=\"$width\" height=\"$height\"")
        if (attrs) {
            attrs.each { key, value ->
                sb.append(" $key=\"$value\"")
            }
        }
        sb.append(' />')
        sb.toString()
    }
    
    private def createPetLink(id, size) {
        def params = [ size: size, filename: Image.filename(id, size) ]
        createLink(url: [ controller: 'pet', action: 'view', id: id, params: params ])
    }
}